package com.continuing.web.controller.beef;

import java.util.List;

import com.continuing.common.core.domain.model.LoginUser;
import com.continuing.framework.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.continuing.common.annotation.Log;
import com.continuing.common.core.controller.BaseController;
import com.continuing.common.core.domain.AjaxResult;
import com.continuing.common.enums.BusinessType;
import com.continuing.beef.domain.type.BillType;
import com.continuing.beef.service.type.IBillTypeService;
import com.continuing.common.utils.poi.ExcelUtil;
import com.continuing.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 账单类型Controller
 *
 * @author ruoyi
 * @date 2021-09-16
 */
@RestController
@RequestMapping("/beef/bill/type")
public class BillTypeController extends BaseController {

    @Autowired
    private IBillTypeService billTypeService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询账单类型列表
     */
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('system:type:list')")
    public TableDataInfo list(BillType billType, HttpServletRequest request) {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(request);
        List<BillType> list = billTypeService.selectBillTypeList(billType, loginUser);
        return getDataTable(list);
    }

    /**
     * 相较与list没有进行分页
     *
     * @param billType
     * @param request
     * @return
     */
    @GetMapping("/getAllList")
    @PreAuthorize("@ss.hasPermi('system:type:list')")
    public TableDataInfo getAllList(BillType billType, HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        List<BillType> list = billTypeService.selectBillTypeList(billType, loginUser);
        return getDataTable(list);
    }

    /**
     * 选择用到的 账单类型 数据
     *
     * @param request
     * @return
     */
    @GetMapping("/getAllListBySelect")
    @PreAuthorize("@ss.hasPermi('system:type:list')")
    public TableDataInfo getAllListBySelect(HttpServletRequest request) {
        return getDataTable(
                billTypeService.selectBillTypeSelectListByDeptId(
                        // 获取部门id
                        tokenService.getLoginUser(request).getDeptId()
                )
        );
    }


    /**
     * 导出账单类型列表
     */
    @GetMapping("/export")
    @PreAuthorize("@ss.hasPermi('system:type:export')")
    @Log(title = "账单类型", businessType = BusinessType.EXPORT)
    public AjaxResult export(BillType billType, HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        List<BillType> list = billTypeService.selectBillTypeList(billType, loginUser);
        ExcelUtil<BillType> util = new ExcelUtil<BillType>(BillType.class);
        return util.exportExcel(list, "账单类型数据");
    }

    /**
     * 获取账单类型详细信息
     */
    @GetMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPermi('system:type:query')")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(billTypeService.selectBillTypeById(id));
    }

    /**
     * 新增账单类型
     */
    @PreAuthorize("@ss.hasPermi('system:type:add')")
    @Log(title = "账单类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BillType billType, HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        return toAjax(billTypeService.insertBillType(billType, loginUser));
    }

    /**
     * 修改账单类型
     */
    @PutMapping
    @PreAuthorize("@ss.hasPermi('system:type:edit')")
    @Log(title = "账单类型", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@RequestBody BillType billType, HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        billType.setUpdateBy(loginUser.getUser().getUserId() + "");
        return toAjax(billTypeService.updateBillType(billType));
    }

    /**
     * 删除账单类型
     */
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPermi('system:type:remove')")
    @Log(title = "账单类型", businessType = BusinessType.DELETE)
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(billTypeService.deleteBillTypeByIds(ids));
    }
}
